<style scoped lang="scss">

</style>

<template>
  <div>

    <el-form :inline="true" :model="queryParams" class="demo-form-inline">
      <el-form-item label="分类名称 : ">
        <el-input v-model="queryParams.cthrName" placeholder="分类名称"></el-input>
      </el-form-item>
      <el-form-item label="分类代号 : ">
        <el-input v-model="queryParams.cthrCode" placeholder="分类代号"></el-input>
      </el-form-item>
      <el-form-item label="序号 从">
        <el-input v-model="queryParams.cthrNumFrom" placeholder="从"></el-input>
      </el-form-item>
      <el-form-item label="到">
        <el-input v-model="queryParams.cthrNumTo" placeholder="到"></el-input>
      </el-form-item>
      <el-form-item label="二级分类 : ">
        <el-select v-model="queryParams.ctwoId" placeholder="二级分类">
          <el-option v-for="item in ctwoList"
                     :key="item.ctwoId"
                     :label="item.ctwoName"
                     :value="item.ctwoId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="一级分类 : ">
        <el-checkbox-group v-model="checkedList">
          <el-checkbox v-for="item in coneList" :label="item.coneId">
            {{ item.coneName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="query">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" @click="handleAdd" plain icon="el-icon-plus" size="mini">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" @click="handleDelete()" plain icon="el-icon-delete" size="mini">删除</el-button>
      </el-col>
    </el-row>

    <br>

    <el-table :data="tableData"
              v-loading="loading"
              stripe border
              @selection-change="handleSelectionChange">

      <el-table-column
        type="selection"
        width="55">
      </el-table-column>

      <el-table-column prop="cthrId" label="三级分类主键">
      </el-table-column>
      <el-table-column prop="coneName" label="一级分类">
      </el-table-column>
      <el-table-column prop="ctwoName" label="二级分类">
      </el-table-column>
      <el-table-column prop="cthrName" label="分类名称">
      </el-table-column>
      <el-table-column prop="cthrNum" label="分类序号">
      </el-table-column>
      <el-table-column prop="cthrCode" label="分类代号">
      </el-table-column>
      <el-table-column prop="cthrInfo" label="分类说明">
      </el-table-column>

    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageNum"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%" append-to-body>

      <el-form ref="dialogForm" :model="dialogForm" :rules="dialogRules" lable-width="80px">

        <el-form-item prop="repositoryName" label="仓库名称 : ">
          <el-input v-model="dialogForm.repositoryName" placeholder="仓库名称"></el-input>
        </el-form-item>
        <el-form-item prop="repositoryCode" label="仓库代号 : ">
          <el-input v-model="dialogForm.repositoryCode" placeholder="仓库代号"></el-input>
        </el-form-item>
        <el-form-item prop="repositoryPhone" label="联系电话 : ">
          <el-input v-model="dialogForm.repositoryPhone" placeholder="仓库代号"></el-input>
        </el-form-item>
        <el-form-item prop="repositoryState" label="仓库状态 : ">
          <el-radio-group v-model="dialogForm.repositoryState">
            <el-radio :label=0>停用</el-radio>
            <el-radio :label=1>启用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="repositoryAddress" label="仓库地址 : ">
          <el-input v-model="dialogForm.repositoryAddress" placeholder="仓库代号"></el-input>
        </el-form-item>
        <el-form-item prop="repositoryStartDate" label="启动时间 : ">
          <el-date-picker type="date" v-model="dialogForm.repositoryStartDate" placeholder="开始日期">
          </el-date-picker>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit">确 定</el-button>
      </span>

    </el-dialog>

  </div>
</template>

<script>
import axios from "axios";
import {editPage, dialogSubmit, getBasePage, deleteBase} from '@/api/webshop/CategoryThree.js'
import {getBasePage as getConeBasePage} from '@/api/webshop/CategoryOne.js'
import {getBasePage as getCtwoBasePage} from '@/api/webshop/CategoryTwo.js'

export default {
  name: "categoryThree",
  components: {
    axios
  },
  created() {
    this.getConeList()
    this.getCtwoList()
    this.getList()
  },
  data() {
    return {
      tableData: [],
      coneList: [],
      ctwoList: [],
      coneFullList: [],
      ctwoFullList: [],
      title: "",
      total: 0,
      checkedList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cthrName: null,
        cthrCode: null,
        cthrNumFrom: null,
        cthrNumTo: null,
        ctwoId: null,
        coneIds: null
      },

      dialogForm: {
        repositoryName: null,
        repositoryCode: null,
        repositoryPhone: null,
        repositoryStartDate: null,
        repositoryState: null,
        repositoryAddress: null
      },
      dialogRules: {
        repositoryName: [
          {required: true, message: '请输入仓库名称', trigger: 'blur'},
          {min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'}
        ],
        repositoryCode: [
          {required: true, message: '请输入仓库代号', trigger: 'blur'}
        ],
        repositoryAddress: [
          {min: 0, max: 500, message: '长度在 500 个字符以内', trigger: 'blur'}
        ]
      },

      loading: false,
      dialogVisible: false

    }
  },
  methods: {
    query() {
      this.pageNum = 1
      this.queryParams.coneIds = this.checkedList.join(",")
      console.log("queryParams", this.queryParams)
      this.getList()
    },
    handleSizeChange(pageSize) {
      console.log("pageSize", pageSize)
      this.queryParams.pageSize = pageSize
      this.getList()
    },
    handleCurrentChange(pageNum) {
      console.log("pageNum", pageNum)
      this.queryParams.pageNum = pageNum
      this.getList()
    },

    handleSelectionChange(val) {
      this.selectedIds = val.map(item => item.cthrId)
      console.log(this.selectedIds)
    },
    dateFormat(row, column, cellValue, index) {
      return cellValue ? cellValue.substring(0, 10) : cellValue
    },
    infoFormat(row, column, cellValue, index) {
      return cellValue.length > 10 ? cellValue.substring(0, 10) + "..." : cellValue
    },
    getList() {
      this.loading = true
      getBasePage(this.queryParams).then(res => {
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    onSubmit() {
      console.log('submit!')
      console.log(this.dialogForm)
      this.$refs["dialogForm"].validate((valid) => {
        if (valid) {

          this.loading = true

          dialogSubmit(this.dialogForm, this.title).then(res => {
            if (res.code != 200) {
              this.$message({
                message: '添加失败',
                type: 'warning'
              });
              return
            }
            this.$message({
              message: '添加成功',
              type: 'success'
            });
            this.dialogVisible = false
            this.loading = false
            this.getList()

          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })

    },
    handleAdd() {
      this.resetDialogForm()
      this.title = "添加"
      this.dialogVisible = true
    },
    handleEdit(id) {
      this.loading = true
      editPage(id).then(res => {
        console.log(res);
        this.dialogForm = res.data
        this.title = "修改"
        this.loading = false
        this.dialogVisible = true
      })
    },
    handleDelete(id) {
      let ids = id || this.selectedIds.join(",")
      if (!ids) {
        this.$message({
          message: '请选择要删除的项',
          type: 'warning'
        });
        return
      }

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBase(ids)
          .then(res => {
            console.log(res);
            if (res.code != 200) {
              this.$message({
                message: '删除失败',
                type: 'warning'
              });
              return
            }
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.getList()
          })
          .catch(err => {
            this.$message({
              message: '删除异常',
              type: 'error'
            });
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });


    },
    getConeList() {
      this.loading = true
      getConeBasePage().then(res => {
        this.coneFullList = res.data
        this.loading = false
      })
    },
    getCtwoList() {
      this.loading = true
      getCtwoBasePage().then(res => {
        this.ctwoFullList = res.data
        this.loading = false
      })
    }

  }
}
</script>
